﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.DataStructure
{
    public class TwoWayDictionary<T1, T2>
    {
        Dictionary<T1, T2> dic1 = new Dictionary<T1, T2>();
        Dictionary<T2, T1> dic2 = new Dictionary<T2, T1>();

        public void Add(T1 value1, T2 value2, bool replaceIfExist)
        {
            if (!dic1.ContainsKey(value1))
                dic1.Add(value1, value2);
            else if (replaceIfExist)
                dic1[value1] = value2;

            if (!dic2.ContainsKey(value2))
                dic2.Add(value2, value1);
            else if (replaceIfExist)
                dic2[value2] = value1;
        }

        public bool ContainsType1(T1 value1)
        {
            return dic1.ContainsKey(value1);
        }

        public bool ContainsType2(T2 value1)
        {
            return dic2.ContainsKey(value1);
        }

        public T2 GetByType1(T1 value1)
        {
            return dic1[value1];
        }

        public T1 GetByType2(T2 value1)
        {
            return dic2[value1];
        }
    }
}
